<template>
    <page-view>
        <el-card>
            <template #header>
                <div class="flex flex-row justify-between items-center">
                    <div class="font-bold">新增菜品分类</div>
                    <el-link type="primary" @click="$router.replace({ name: 'CategoryInfoList' })">返回列表</el-link>
                </div>
            </template>
            <el-form :model="AddCategoryFormData" ref="AddCategoryEl" :rules="AddCategoryRules">
                <el-form-item label="分类名称" prop="category_name">
                    <el-input v-model="AddCategoryFormData.category_name"></el-input>
                </el-form-item>
                <el-form-item class="ml-[70px] mt-[50px]">
                    <el-button type="primary" @click="submitCategoryForm">
                        新增分类
                    </el-button>
                    <el-button type="success" @click="AddCategoryFormData.category_name = ''">
                        重置
                    </el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </page-view>
</template>

<script setup>
import { reactive, ref } from "vue"
import API from "../../utils/API";
import { ElNotification,ElMessage } from 'element-plus'
import { useRouter } from "vue-router"
const AddCategoryFormData = reactive({
    category_name: "",
})
const AddCategoryEl = ref(null);
const AddCategoryRules = {
    category_name: [{ required: true, message: "请输入菜品分类", trigger: "blur" }]
}
// 新增表单验证
const submitCategoryForm = () => {
    AddCategoryEl.value.validate(valid => {
        if (valid) {
            console.log("验证通过");
            addCategory();
        }
        else {
            ElMessage({
                message: '验证不通过',
                type: 'warning',
            })
        }
    })
}

const router = useRouter();
const addCategory = () => {
    API.categoryInfo.add(AddCategoryFormData)
        .then(() => {
            ElNotification({
                title: "提示",
                message: "新增成功",
                type: "success"
            })
            router.push({
                name: "CategoryInfoList"
            })
        })
}
</script>


<style scoped lang="scss">

</style>